<template>
	<view class="body">
		<xk-header title="设备维修"></xk-header>
		<view class="list" >
			<view class="list-item header" @click="orderMore = !orderMore">
				<view>维修单号:  {{data.apply_no}}</view>
				<image v-if="!orderMore" src="/static/up.png"></image>
				<image v-else  src="/static/down.png"></image>
			</view>
			<view v-if="orderMore" class="data-item">
				<view class="order-info">
					<view class="info-item"><view class="key">设备信息：</view>
					<text style="word-break:break-all;">{{data.device_code}}-{{data.device_name}}</text>
				</view>
				<view class="info-item"><view class="key">故障描述：</view>{{data.repair_content}}</view>
				<view class="info-item"><view class="key">设备地址：</view>{{data.address || '-'}}</view>
				<view v-if="data.apply_image" class="info-item"><view class="key"></view>
					<image-list :pics="data.apply_image"></image-list>
				</view>
				</view>
			</view>
		</view>
		<view class="list">
			<view class="list-item header" @click="more = !more">
				<view>工单受理</view>
				<image v-if="!more" src="/static/up.png"></image>
				<image v-else  src="/static/down.png"></image>
			</view>
			<view v-if="more">
				<picker v-if="!isWorker" :range="users" :value="userIndex === -1 ? 0 : userIndex" range-key="user_name"
					@change="userChange">
					<view class="list-item">
						<view>跟进人:</view>
						<view :class="userIndex !== -1 ? 'right-title1' : 'right-title1 right-title1-empty'">{{userIndex !== -1 ? users[userIndex].user_name : "请选择"}}</view>
						<image mode="aspectFill" class="jiantou" src="/static/jiantou.png"/>
					</view>
				</picker>
				<view class="list-item textarea">
					<view>备注：</view>
					<textarea placeholder="备注" placeholder-class="placeholder"/>
				</view>
				<!-- <biaofun-datetime-picker :defaultValue="defaultDate"
					start="2000-02-03 00:00"
					end="2200-10-28 23:59"
					fields="minute"
					@change="dealTimeChange"
				>
					<view class="list-item">
						<view class="key not-null">按排上门时间：</view>
						<text :class="planTime ? 'right-title1' : 'right-title1 right-title1-empty'">{{planTime || "请选择"}}</text>
						<image mode="aspectFill" class="jiantou" src="/static/jiantou.png"/>
					</view>
				</biaofun-datetime-picker> -->
				<!-- <picker mode="date" :value="planDate"
				 @change="dateChange">
					<view class="list-item">
						<view class="key not-null">按排上门日期：</view>
						<text :class="planDate ? 'right-title1' : 'right-title1 right-title1-empty'">{{planDate || "请选择"}}</text>
						<image mode="aspectFill" class="jiantou" src="/static/jiantou.png"/>
					</view>
				</picker>
				<picker mode="time" :value="planTime"
				 @change="timeChange">
					<view class="list-item">
						<view class="key not-null">按排上门时间：</view>
						<text :class="planTime ? 'right-title1' : 'right-title1 right-title1-empty'">{{planTime || "请选择"}}</text>
						<image mode="aspectFill" class="jiantou" src="/static/jiantou.png"/>
					</view>
				</picker> -->
			</view>
		</view>
		<view class="common-bottom-view">
			<view class="common-btn"  v-if="isWorker" @click="startRepair">接单</view>
			<view class="common-btn" v-else  @click="onDispatch">派单</view>
		</view>
		<view class="common-zw"></view>
	</view>
</template>

<script>
	import moment from 'moment'
	export default {
		data() {
			return {
				repairId: '',

				data: {},
				more: true,
				orderMore: true,
				isWorker: true,
				receiverId: "", 	// 指定人员ID
				userIndex: -1,
				users:[],
			};
		},
		onLoad(options) {
			this.repairId = options.repairId
			this.getData();
		},
		methods:{
			getData(){
				this.$api.baseData_getPdDeviceRepair(this.repairId).then((res)=>{
					this.data = res.task
					if (this.data.status === "20" &&
						this.$util.getRights().pd_device_repair_taking) {
						this.isWorker = true
					}
					if (this.data.status === '20' &&
						this.$util.getRights().pd_device_repair_dispatch) {
						this.isWorker = false
						this.initInfo()
					}
				})
			},
			initInfo() {
				this.$api.baseData_getPdDeviceRepairWorkerList().then(res => {
					console.warn("维修人员列表：", res)
					this.users = res
				})
			},
			userChange(e) {
				this.userIndex = e.detail.value;
				this.receiverId = this.users[this.userIndex].user_id
			},

			onDispatch() {
				if (!this.receiverId) {
					this.$util.toast("请选择指定人员")
					return
				}
				this.$util.confirm({
					content: "确定给 '"+this.users[this.userIndex].user_name+"' 进行接单操作？",
					confirmText: "派单"
				}).then(isConfirm => {
					if (isConfirm) {
						this.$util.post({
							url: "app/pdDeviceRepair/dispatch",
							data: {
								repair_id: this.data.repair_id,
								receiver_id: this.receiverId,	// 指定人员ID
							}
						}).then(res => {
							uni.$emit('refreshHomeDataList')
							uni.$emit('refreshDeviceRepair')
							uni.redirectTo({
								url: '/pagesA/facility-fix/facility-fix-finish?type=accept'
							})
						})

					}
				})
			},
			startRepair(){
				this.$util.confirm({
					content: "确定要开始接单？",
					confirmText: "确定接单"
				}).then(isConfirm => {
					if (isConfirm) {
						this.$util.post({
							url: "app/pdDeviceRepair/taking",
							data: {
								repair_id: this.data.repair_id,
							}
						}).then(res => {
							uni.$emit('refreshHomeDataList')
							uni.$emit('refreshDeviceRepair')
							uni.redirectTo({
								url: '/pagesA/facility-fix/facility-fix-finish?type=accept'
							})
						})

					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
@import '@/common/from.scss';

</style>
